package com.stu.day13;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * List的add()和remove()方法每次都会让modcount++
 * 增强for循环和迭代器遍历时会检测modcount是否变化,
 * 如果进行add()或remove()操作,导致modcount变化会报错
 * @author zy
 * @date 2025/2/11 13:32
 */
public class ListRemoveDemo {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        User user1 = new User(1,"one");
        User user2 = new User(2,"two");
        User user3 = new User(3,"three");
        User user4 = new User(4,"one");
        User user5 = new User(1,"five");
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        //contains判断集合是否含有某元素
        System.out.println(list.contains(user5));  //user5和user1的id一致,所以true

        list.add(user5);
//        list.remove(user5);  // 移除user5即移除id为1的第一个元素,即user1
//        for (User user : list) {
//            System.out.println(user);
//        }

//        removeIterator(list,"one");  // 自定义函数,删除name为one的元素
//        removeJDK8(list,"one");
//        removeFor(list,"one");
        removeByUsername(list,"one");

        Iterator<User> iterator = list.iterator();
        while (iterator.hasNext()) {
            User user = iterator.next();
            System.out.println(user);
        }
    }

    /**
     * 迭代器中的正确删除方式
     * @param list
     * @param username
     */
    private static void removeIterator(List<User> list, String username) {
        Iterator<User> iter = list.iterator();
        while(iter.hasNext()){
            User user = iter.next();
            if(username.equals(user.getName())){
                //java.util.ConcurrentModificationException
                //list.remove(user);
                iter.remove();  // 会保证modcount的变化不会报错
            }
        }
    }

    /**
     * removeIf方法
     * @param list
     * @param username
     */
    private static void removeJDK8(List<User> list, String username) {
        //jdk8.0特性
        list.removeIf(u->username.equals(u.getName()));
    }

    /**
     * 演示增强for循环的只读性
     * @param list
     * @param username
     */
    private static void removeFor(List<User> list, String username) {
        //准备一个需要删除的集合
        List<User> removeList = new ArrayList<>();
        for (User user : list) {
            if(username.equals(user.getName())){
                //直接在增强for循环体中去执行删除的代码,抛出并发修改的异常
                //不允许边读边修改{删除}
                //java.util.ConcurrentModificationException
                //list.remove(user);
                removeList.add(user);
            }
        }
        //统一进行删除,同时移除多个元素
        list.removeAll(removeList);
    }

    public static void removeByUsername(List<User> list,String username){
        //遍历这个集合
        for (int i = 0; i < list.size(); i++) {
            if(username.equals(list.get(i).getName())){
                list.remove(i);  // 根据下标 进行删除
                i--;  // 防止成功删除一个之后,下标会左移,导致漏删
            }
        }
    }
}


class User{
    private Integer id;
    private String name;

    public User() {
    }

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(id, user.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("User{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
